<?php
/**
 * Created by PhpStorm.
 * User: ml
 * Date: 2017-11-24
 * Time: 15:51
 */
class Worktask extends MY_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->model('M_work_task');
    }

    //获取作业任务列表
    public function ListWorkTask()
    {
        //根据作业岗位属性（分作业岗位和作业人员，有开关判断是岗位还是人员）
        $name = trim($this->input->post('title'));//作业任务名称
        if(!empty($name))
        {
            $name = " where job_name like '%$name%' ";
        }
        $list = $this->M_work_task->tasklist($name);
        foreach ($list as $key=>$value)
        {
            $list[$key]['work'] = $this->M_work_task->getpostname($value['post_type_id'],$value['job_post']);
        }
        exit(json_encode(array('code'=>0,'data'=>$list,'info'=>'成功')));
    }
    //添加作业任务
    public function AddWorkTask()
    {
        /*
         * 【【【【【增加日志】】】】】
         */
        //条件1：岗位的作业岗位智能选择带有岗位属性的作业岗位，作业人员属性的只能选择带该属性的作业岗位
        $data=array();
        $data['job_name'] = trim($this->input->post('title'));//作业任务名称
        $data['job_type'] = $this->input->post('type');//作业任务类型
        $data['post_type_id'] = $this->input->post('status');//作业岗位or作业人员选项 1：作业人员2：作业岗位
        $data['job_post'] = $this->input->post('post_user');//作业岗位or作业人员
        $data['job_content'] = $this->input->post('content');//作业内容
        $data['job_benchmark'] = $this->input->post('standard');//作业标准规范
        $data['is_control'] = $this->input->post('control');//1监控 2不监控
        $data['job_addtime'] =time();

        foreach ($data as $key=>$val)
        {
            if(empty($val))
            {
                exit(json_encode(array('code'=>1,'data'=>"错误1:$key 为$val",'info'=>'数据不全,保存失败')));
            }
        }
        $tsql="select count(*) as num from t_job where job_name='{$data['job_name']}'";
        $res= $this->M_work_task->query_one($tsql);
        if($res['num']>0){
            exit(json_encode(array('code'=>1,'data'=>'','info'=>'作业已存在')));
        }
        $newid = $this->M_work_task->saveData($data);
        if($newid!=false){
            exit(json_encode(array('code'=>0,'data'=>'','info'=>'添加成功')));
        }else{
            exit(json_encode(array('code'=>1,'data'=>'','info'=>'添加失败')));
        }
    }
    /**
     * 修改任务
     */
    public function Savetask(){
        /*
         * 【【【【【增加日志】】】】】
         */
        $data['job_id'] = $this->input->post('id');//任务id
        $data['job_name'] = trim($this->input->post('title'));//作业任务名称
        $data['job_type'] = $this->input->post('type');//作业任务类型
        $data['post_type_id'] = $this->input->post('status');//作业岗位or作业人员选项 1：作业人员2：作业岗位
        $data['job_post'] = $this->input->post('post_user');//作业岗位or作业人员
        $data['job_content'] = $this->input->post('content');//作业内容
        $data['job_benchmark'] = $this->input->post('standard');//作业标准规范
        $data['is_control'] = $this->input->post('control');//1监控 2不监控
        $tsql="select count(*) as num from t_job where job_name='{$data['job_name']}' and job_id<>{$data['job_id']}";
        $re= $this->M_work_task->query_one($tsql);
        if($re['num']>0){
            exit(json_encode(array('code'=>1,'data'=>'','info'=>'作业已存在')));
        }
        foreach ($data as $key=>$val)
        {
            if(empty($val))
            {
                exit(json_encode(array('code'=>1,'data'=>"错误1:$key 为$val",'info'=>'数据不全,保存失败')));
            }
        }
        $res = $this->M_work_task->updateData($data,array('job_id'=>$data['job_id']));
        if($res!=false){
            exit(json_encode(array('code'=>0,'data'=>'','info'=>'修改成功')));
        }else{
            exit(json_encode(array('code'=>1,'data'=>'','info'=>'修改失败')));
        }
    }
    //编辑作业任务
    public function EditWorkTask()
    {
        //条件1：岗位的作业岗位智能选择带有岗位属性的作业岗位，作业人员属性的只能选择带该属性的作业岗位
        $workid = $this->input->post('id');//任务id
        $res = $this->M_work_task->getwork($workid);
        exit(json_encode(array('code'=>0,'data'=>$res,'info'=>'成功')));

    }
    //删除作业任务（逻辑删除）
    public function DeleteWorkTask()
    {
        /*
         * 【【【【【增加日志】】】】】
         */
        //条件1 先删作业流程才可删除作业任务
        $uid = $this->input->post('id');//任务id
        $count = $this->M_work_task->isdelwork($uid);
        if(!empty($count))
        {
            exit(json_encode(array('code'=>1,'data'=>'失败','info'=>'请您先删除作业流程当中的任务')));
        }
        $res = $this->M_work_task->delData(array('job_id'=>$uid));
        if($res){
            exit(json_encode(array('code'=>0,'data'=>'','info'=>'删除成功')));
        }
        exit(json_encode(array('code'=>1,'data'=>'','info'=>'删除失败')));
    }
    //获取作业任务列表（id、ame）
    public function WorkTaskList()
    {
        $res1 = $this->M_work_task->getWorkTaskList();//获取作业任务列表
        exit(json_encode(array('code'=>0,'data'=>$res1,'info'=>'成功')));
    }
    //获取作业流程列表
    public function WorkProcessList()
    {
//        $res2 = $this->M_work_task->getWorkWPList();//获取作业岗位带负责人、作业车间

        $sel = getParam($this->input->get_post("sel"), 'int', 0);
        $res3 = $this->M_work_task->getWorkProcessList();//获取作业流程列表
        if($sel==1)
        {
            foreach ($res3 as $key=>$value)
            {
                $data[$key]['id'] = $value['job_process_id'];
                $data[$key]['text'] = $value['job_process_name'];
            }
            $res3 = $data;
        }
        exit(json_encode(array('code'=>0,'data'=>$res3,'info'=>'成功')));

    }
    //根据作业流程ID获取详情
    public function GetWorkProcess()
    {
        $workid = $this->input->post('id');//作业流程id
        $res = $this->M_work_task->getWPDetailById($workid);
        exit(json_encode(array('code'=>0,'data'=>$res,'info'=>'成功')));
    }
    //添加作业流程
    public function AddWorkProcess()
    {
        //获取作业任务ID
        $data['job_process_name'] = trim($this->input->post('name'));//作业流程名称
        $data['arr_str'] = $this->input->post('task_id');//作业流程的作业任务ID
//        $data['arr_str'] = implode(',',$data['arr_str']);
//        $data['arr_str'] = array('0'=>'260,8');

        $rs = $this->M_work_task->insertWorkProcess($data);

        if($rs > 0){
            exit(json_encode(array('code'=>0,'data'=>'','info'=>'添加成功')));
        }else if ($rs == '-1'){
            exit(json_encode(array('code'=>1,'data'=>'','info'=>'具有相同任务名')));
        }
        exit(json_encode(array('code'=>1,'data'=>'','info'=>'异常')));
    }
    //修改作业流程
    public function UpdateWorkProcess()
    {
        //获取作业任务ID
        $data['job_process_id'] = trim($this->input->post('id'));//作业流程ID
        $data['job_process_name'] = trim($this->input->post('name'));//作业流程名称
        $data['arr_str'] = $this->input->post('task_id');//作业流程的作业任务ID
//        $data['job_process_id'] = 46;//作业流程ID
//        $data['job_process_name'] = '123123';//作业流程名称
//        $data['arr_str'] = array(0=>"0,8",1=>"0,9");//作业流程的作业任务ID
//        exit(json_encode(array('code'=>1,'data'=>'','info'=>'未做完')));
        $rs = $this->M_work_task->updateWorkProcess($data);

        if($rs > 0){
            exit(json_encode(array('code'=>0,'data'=>'','info'=>'修改成功')));
        }
        exit(json_encode(array('code'=>1,'data'=>'','info'=>'修改失败')));
    }
    /**
     * 删除作业流程
     * 2017-1-9
     */
    public function deleteWorkProcess(){
        /*
         * 【【【【【增加日志】】】】】
         */
        $_id = $_POST['id'];
        $flag = $this->M_work_task->deleteWorkProcess($_id);
        if($flag){
            exit(json_encode(array('code'=>0,'data'=>'','info'=>'删除成功')));
        }else{
            exit(json_encode(array('code'=>1,'data'=>'','info'=>'删除失败')));
        }
    }
}